import axios from "axios";
import Vue from 'vue';

export default {
    namespaced: true,
    state: {
        locations: [],
        industry: []
    }, getters: {
        getLocations(state) {
            return state.locations;
        },
        getIndustry(state) {
            return state.industry;
        }
    }, mutations: {
        //注入地区数据
        importLocation(state, response) {
            Vue.set(state, 'locations', response);
        },
        //注入行业数据
        importIndustry(state, response) {
            Vue.set(state, 'industry', response);
        }
    }, actions: {
        async initializeNavigation({commit}) {
            //获取地区导航和行业导航
            //如果本地会话有数据,就从本地会话读取
            let locations = JSON.parse(sessionStorage.getItem("locations"));
            if (!locations) {
                let {data: rs} = await axios.get('filterInfo/getLocation');
                locations = rs;
                //保存至本地会话,避免反复读取,导致消耗多余性能
                sessionStorage.setItem("locations", JSON.stringify(rs));
            }
            let industry = JSON.parse(sessionStorage.getItem("industry"));
            if (!industry) {
                let {data: rs} = await axios.get('filterInfo/getIndustry');
                industry = rs;
                sessionStorage.setItem("industry", JSON.stringify(rs));
            }
            commit('importLocation', locations);
            commit('importIndustry', industry);
        },
        async resetNavigation({dispatch}) {
            sessionStorage.removeItem("industry");
            dispatch('initializeNavigation');
        }
    }
}